<template>
	<view>
		<uni-popup ref="mtPicker">
			<view class="w-picker-m">
				<view>
					<text @click="mtPicker.close()"> {{$t('calendar.cancel')}} </text>
					<text>{{title || $t('webAuto.PleaseSelect')}}</text>
					<text @click="ok">{{$t('calendar.sure')}}</text>
				</view>
				<scroll-view scroll-y class="w-picker-content">
					<checkbox-group class="w-picker-options" @change="checkChange">
						<label v-for="(option, i) in props.options" :key="i">
							<checkbox :value="option.value" :checked="checked(option.value)" color="#4478F7"
								style="transform:scale(0.7); flex: 1;" />
							<text>{{option[index]}}</text>
						</label>
					</checkbox-group>
				</scroll-view>
			</view>
		</uni-popup>
		<slot :show="show"></slot>
	</view>
</template>

<script setup>
	import { computed, ref, watch } from "vue";
	const mtPicker = ref()
	const props = defineProps({
		title: String,
		options: {
			type: Array,
			default: () => {
				return []
			}
		},
		index: {
			type: String,
			default: 'text'
		},
		modelValue: Array
	})

	const val = ref([])
	const valSet = ref(null)

	const emits = defineEmits(['update:modelValue'])

	function show() {
		val.value = props.modelValue || []
		mtPicker.value.open('bottom')
	}

	function ok() {
		emits('update:modelValue', val.value)
		mtPicker.value.close()
	}

	function checkChange(e) {
		val.value = e.detail.value
	}

	function checked(v) {
		return val.value.indexOf(v) > -1
	}
</script>

<style lang="less" scoped>
	.w-picker-m {
		height: 300px;
		width: 100%;
		background-color: white;

		.w-picker-options {
			display: flex;
			flex-direction: column;
			padding: 10px;

			&>label {
				padding: 5px 0;
				margin: 2px 0;
				background-color: #f4f4f4;
				border-radius: 5px;
				display: flex;
				align-items: center;
			}
		}

		&>view:first-child {
			padding: 10px 8px;
			display: flex;

			&>text:nth-child(1) {
				color: #8a8a8a;
			}

			&>text:nth-child(2) {
				flex: 1;
				text-align: center;
			}

			&>text:nth-child(3) {
				color: #4478F7;
			}
		}

		.w-picker-content {
			height: 260px;
		}
	}
</style>